angular.module('Controller')

.controller('WFAuthorityManageEditCtrl', ($scope,NgTableParams,wfAlert, commonUtil,menuManage,commonConstant,$rootScope,$uibModal,$http) ->
    angular.noop()
    $scope.type = $rootScope.pageType
    $scope.data=$rootScope.pageParams
    $scope.resourceData=[]
    $scope.typeList=commonConstant.authorizationTypeList()
    $scope.enabledList=commonConstant.enabledList()
    $scope.resourceTypeList=commonConstant.resourceTypeList()
    return if $scope.data==undefined
    $scope.tableParams = new NgTableParams(
        {page: 1, count: commonConstant.pageTableCount},
        {
            counts: [5, 10, 20],
            getData: ($defer, params)->
                 $scope.doQuery($defer,params)
                 return ;
        }
    )
    $scope.doQuery = ($defer, params)->
        requestData = {
            page: params.page() - 1,
            size: params.count(),
        }
        getParam = commonUtil.transferGetParam(requestData)
        $http.get('@ostengar/authority/'+$scope.data.id+'/resources'+getParam)
        .success (resp)->
            if resp.code == 0
                $scope.tableData = resp.message.content;
                params.total(resp.message.totalElements);
                $defer.resolve($scope.tableData);
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    $scope.modalEnableList=commonConstant.enabledList()
    $scope.resetAuthority=()->
        $scope.data.authority=''
        $scope.data.description=''
        $scope.data.type=''
        $scope.data.enabled=''
    $scope.newAuthority=()->
        postData={
            authority:$scope.data.authority
            description:$scope.data.description
            enabled:$scope.data.enabled
            type:$scope.data.type
        }
        getParams=commonUtil.transferGetParam(postData)
        $http.post('@ostengar/authority',postData)
        .success((resp)->
            if(resp.code==0)
                wfAlert.toastInfo('新增权限成功')
            else
                wfAlert.toastError('新增权限失败：',resp.message)
        )
    $scope.editAuthority=()->
        putData={
            accountId:2
            authority:$scope.data.authority
            description:$scope.data.description
            enabled:$scope.data.enabled
            type:$scope.data.type
        }
        getParams=commonUtil.transferGetParam(putData)
        $http.put('@ostengar/authority/'+$scope.data.id,putData)
        .success((resp)->
            if(resp.code==0)
                wfAlert.toastInfo('修改权限成功')
            else
                wfAlert.toastError('修改权限失败：',resp.message)
        )
    $scope.saveSetting=()->
        if $scope.type=='new'
            $scope.newAuthority()
        else
            $scope.editAuthority()
    $scope.bindResource=()->
        showModal()
    $scope.queryData=(firstPage)->
        if(firstPage == true)
            $scope.tableParams.page(1)
        $scope.tableParams.reload()
    $scope.removeResource=(item)->
        requestData={
            resources:item.id
        }
        getParam=commonUtil.transferGetParam(requestData)
        $http.delete('@ostengar/authority/'+$scope.data.id+'/resources'+getParam)
        .success (resp)->
            console.log(resp.message)
            $scope.queryData()
        .error (err)->
            console.log(err)
    showModal=()->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'WFAuthorityBindingResourceEditCtrl',
            templateUrl: 'templates/system/authority/authority-binding-resource.html',
            resolve:{
                items: ()->{authorityID:$scope.data.id,data:$scope.tableData}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)